package com.smz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smz.entity.Menu;
import com.smz.entity.TMenu;
import com.smz.service.TMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@Api
@RestController
public class TMenuController {

    @Autowired
    private TMenuService tMenuService;

    @ApiOperation("树形菜单数据")
    @RequestMapping(value = "/tMenu/tree",method = RequestMethod.GET)
    public ResponseEntity<List<TMenu>> findTMenuTree(){
        List<TMenu> listTMenu = tMenuService.findAllTMenu();

        return ResponseEntity.ok(listTMenu);
    }

    @ApiOperation("查询所有仓库")
    @RequestMapping(value = "/tMenu",method = RequestMethod.GET)
    public ResponseEntity<List<TMenu>> findAllTMenu(){
        List<TMenu> listTMenu = tMenuService.list();
        return ResponseEntity.ok(listTMenu);
    }


    @ApiOperation("分页查询查询仓库")
    @RequestMapping(value = "/pageTMenu/{current}/{rows}",method = RequestMethod.GET)
    public ResponseEntity<Page<TMenu>> findPageTMenu(@PathVariable("current")Integer current, @PathVariable("rows")Integer rows){
        Page<TMenu> pageTMenu = tMenuService.findPageTMenus(current, rows,null,null);
        return ResponseEntity.ok(pageTMenu);
    }

    @ApiOperation("增加仓库")
    @RequestMapping(value = "/pageTMenu",method = RequestMethod.POST)
    public String addTMenu(@RequestBody TMenu tMenu){
        tMenuService.save(tMenu);
        return "ok";
    }
    @ApiOperation("查找所有仓库")
    @GetMapping("/findOneWh")
    public ResponseEntity<List<TMenu>> findOneWh() {

        List<TMenu> list = tMenuService.list();

        return ResponseEntity.ok(list);
    }
    @ApiOperation("更新仓库")
    @RequestMapping(value = "/pageTMenu",method = RequestMethod.PUT)
    public String modifyTMenu(@RequestBody TMenu tMenu){
        tMenuService.update(tMenu,new QueryWrapper<TMenu>().lambda().eq(TMenu::getId,tMenu.getId()));
        return "ok";
    }

    @ApiOperation("删除仓库")
    @RequestMapping(value = "/pageTMenu/{id}",method = RequestMethod.DELETE)
    public String modifyTMenu(@PathVariable("id") Integer id){
        tMenuService.removeById(id);
        return "ok";
    }

    @ApiOperation("查询仓库")
    @RequestMapping(value = "/pageTMenu",method = RequestMethod.GET)
    public ResponseEntity<Page<TMenu>> modifyTMenu(@RequestParam(value = "tMenuId",required = false)Integer tMenuId,@RequestParam(value = "detetime",required = false)String detetime){
        if (detetime.equals("null")){
            Page<TMenu> pageTMenu = tMenuService.findPageTMenus(1, 1, tMenuId, null);
            return ResponseEntity.ok(pageTMenu);
        }else {
            Page<TMenu> pageTMenu = tMenuService.findPageTMenus(1, 1, tMenuId, detetime);
            return ResponseEntity.ok(pageTMenu);
        }
    }

    @ApiOperation("通过仓库id查找仓库")
    @GetMapping("/findOneMenu/{id}")
    public ResponseEntity<TMenu> findOneWh(@PathVariable("id")Integer id) {

        TMenu byId = tMenuService.getById(id);

        return ResponseEntity.ok(byId);
    }


    @ApiOperation("查询所有仓库")
    @GetMapping("menus")
    public ResponseEntity<List<TMenu>> findAllMenus(){
        List<TMenu> listTMenu = tMenuService.findAllMenu();
        return ResponseEntity.ok(listTMenu);
    }

}
